package com.fsq.component;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fsq.beans.consts.Constant;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author fsq
 * @date 2020/07/27
 */
@Component
public class SmsComponent {

    private DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
            Constant.ACCESS_KEY_ID,
            Constant.SECRET_ACCESS_KEY);

    private IAcsClient client = new DefaultAcsClient(profile);

    public String SendSms() throws ClientException {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("TemplateCode", Constant.SMS_TEMPLATE);
        request.putQueryParameter("SignName", Constant.SIGN);
        request.putQueryParameter("PhoneNumbers", "18316102612");
        Integer code = new Random().nextInt(1000) * 666;
        String demo = "{\"code\":\"" + code.toString() + "\"}";
        request.putQueryParameter("TemplateParam", demo);

        CommonResponse response = client.getCommonResponse(request);
        return response.getData();

    }

}
